package com.mflex.mwms.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.mwms.R;
import com.mflex.mwms.adapter.UnBoxingAdapter;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.UnBoxingPackInfo;
import com.mflex.mwms.model.UnBoxingRequest;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.CollectionUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UnBoxingActivity extends BaseActivity {
    @BindView(R.id.stv_BoxNumber)
    EditText stvBoxNumber;

    @BindView(R.id.stv_PackNo)
    EditText stvPackNo;

    @BindView(R.id.lvPackList)
    ListView lvPackList;

    @BindView(R.id.button_save)
    Button buttonSave;

    @BindView(R.id.button_clear)
    Button buttonClear;

    MiniLoadingDialog mMiniLoadingDialog;

    List<UnBoxingPackInfo> unBoxingPackInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_un_boxing);

        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        unBoxingPackInfoList = new ArrayList<>();
        UnBoxingAdapter unBoxingAdapter = new UnBoxingAdapter(this, unBoxingPackInfoList);
        lvPackList.setAdapter(unBoxingAdapter);

        stvBoxNumber.setOnKeyListener(this);
        stvPackNo.setOnKeyListener(this);

        buttonSave.setOnClickListener(this);
        buttonClear.setOnClickListener(this);
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_BoxNumber:

                    getPackList(view);
                    break;
                case R.id.stv_PackNo:
                    checkPackNo(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_save:
                doSubmit(view);
                break;

            default:
                break;
        }
    }

    private void getPackList(View v) {
        mMiniLoadingDialog.show();
        XHttp.get("api/UnBoxing/GetPdaPackList")
                .params("BoxNumber", stvBoxNumber.getText().toString())
                .params("Warehouse", MMKVUtils.getString("Warehose", ""))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<List<UnBoxingPackInfo>>, List<UnBoxingPackInfo>>(new SimpleCallBack<List<UnBoxingPackInfo>>() {

                    @Override
                    public void onSuccess(List<UnBoxingPackInfo> response) {
                        mMiniLoadingDialog.dismiss();
                        stvPackNo.setText("");
                        unBoxingPackInfoList = response;
                        UnBoxingAdapter unBoxingAdapter = new UnBoxingAdapter(UnBoxingActivity.this, unBoxingPackInfoList);
                        lvPackList.setAdapter(unBoxingAdapter);
                        stvPackNo.requestFocus();

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(UnBoxingActivity.this, e);
                    }
                }) {
                });
    }

    private void doSubmit(View v) {
        UnBoxingRequest unBoxingRequest = new UnBoxingRequest();
        unBoxingRequest.setBoxNumber(stvBoxNumber.getText().toString());
        List<String> list = new ArrayList<>();
        int qty = 0;
        for (UnBoxingPackInfo info : unBoxingPackInfoList) {
            if (info.isChecked()) {
                qty = qty + 1;
                list.add(info.getPackNo());
            }
        }
        unBoxingRequest.setUnBoxingQty(qty);
        if (CollectionUtils.isEmpty(list)) {
//            XToastUtils.error("没有选中包");
            MessageUtils.errorMsgAndVoice(UnBoxingActivity.this, "没有选中包");
            return;
        }
        unBoxingRequest.setUnBoxingList(list);
        unBoxingRequest.setWarehouse(MMKVUtils.getString("Warehose", ""));
        unBoxingRequest.setUnBoxingReason("PDA拆箱");
        mMiniLoadingDialog.show();
        XHttp.post("api/UnBoxing/PdaUnBoxing")
                .upJson(JsonUtil.toJson(unBoxingRequest))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String response) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxNumber.setText("");
                        stvPackNo.setText("");
                        stvBoxNumber.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                stvBoxNumber.requestFocus();
                            }
                        }, 200);
                        XToastUtils.success("拆箱成功！", 2500);
                        unBoxingPackInfoList = new ArrayList<>();
                        UnBoxingAdapter unBoxingAdapter = new UnBoxingAdapter(UnBoxingActivity.this, unBoxingPackInfoList);
                        lvPackList.setAdapter(unBoxingAdapter);

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
//                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(UnBoxingActivity.this, e);
                    }
                }) {
                });
    }

    private void checkPackNo(View v) {
        if (StringUtils.isEmpty(stvBoxNumber.getText().toString())) {
//            XToastUtils.error("箱号未输入", 2000);
            MessageUtils.errorMsgAndVoice(UnBoxingActivity.this, "箱号未输入");
            stvPackNo.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stvPackNo.requestFocus();
                }
            }, 200);
            return;
        }
        boolean flag = false;
        for (UnBoxingPackInfo unBoxingPackInfo : unBoxingPackInfoList) {
            if (StringUtils.equals(unBoxingPackInfo.getPackNo(), stvPackNo.getText().toString())) {
                unBoxingPackInfo.setChecked(true);
                flag = true;
            }
        }
        if (!flag) {
            stvPackNo.setText("");
            stvPackNo.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stvPackNo.requestFocus();
                }
            }, 200);
//            new MaterialDialog.Builder(v.getContext())
//                    .iconRes(R.drawable.icon_warning)
//                    .title(R.string.tip_warning)
//                    .content("该包不在箱中")
//                    .positiveText(R.string.lab_submit)
//                    .show();
            MessageUtils.errorMsgAndVoice(UnBoxingActivity.this, "该包不在箱中");
            return;
        }
        stvPackNo.setText("");
        UnBoxingAdapter unBoxingAdapter = new UnBoxingAdapter(UnBoxingActivity.this, unBoxingPackInfoList);
        lvPackList.setAdapter(unBoxingAdapter);
    }

}